# -*- coding: utf-8 -*-
"""
Created on Fri Aug 30 13:53:13 2024

@author: Administrator
"""

import tensorflow as tf
import tensorflow.compat.v1 as tf1


def check_tensorflow_gpu():
    print("TensorFlow 版本:", tf.__version__)
    if tf.config.list_physical_devices('GPU'):
        print("GPU is available")
    else:
        print("GPU is not available, using CPU")

    # 测试 GPU 上的简单计算
    try:
        with tf1.Session() as sess:
            with tf.device('/GPU:0'):
                a = tf.constant([1.0, 2.0, 3.0])
                b = tf.constant([4.0, 5.0, 6.0])
                c = a + b
                result = sess.run(c)
                print("GPU 测试计算结果:", result)
    except RuntimeError as e:
        print(f"GPU 计算错误: {e}")


if __name__ == "__main__":
    check_tensorflow_gpu()

"""
TensorFlow 版本: 1.15.0
GPU is available
GPU 测试计算结果: [5. 7. 9.]
"""